"""
@version:python3
@author:大橘为重
@file:18-QAbstractSpinBox-功能测试.py
@time:2022/3/14 21:07
"""
# 0、导入需要的包和模块
from PyQt5.Qt import *
import sys

class MyASB(QAbstractSpinBox):
    def __init__(self,parent = None,num = "0",*args,**kwargs):
        super().__init__(parent,*args,**kwargs)
        self.lineEdit().setText(num)

    def stepEnabled(self):
        # 0--9
        # current_num = int(self.text())
        # if current_num == 0:
        #     return QAbstractSpinBox.StepUpEnabled
        # elif current_num == 9:
        #     return QAbstractSpinBox.StepDownEnabled
        # elif current_num < 0 or current_num > 9:
        #     return QAbstractSpinBox.StepNone
        # else:
        #     return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled
        return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled
    def stepBy(self,p_int):
        # print(p_int)
        current_num = int(self.text()) + p_int
        self.lineEdit().setText(str(current_num))

    def validate(self, p_str, p_int) :# input: str, pos: int 传入文本内容以及光标位置
        # 18-180
        print(p_str)
        try:
            num = int(p_str)
            if num < 18:
                return (QValidator.Intermediate,p_str,p_int)
            elif num <= 180:
                return (QValidator.Acceptable,p_str,p_int)
            else:
                return (QValidator.Invalid, p_str, p_int)
        except:
            if len(p_str) == 0:
                return (QValidator.Intermediate, p_str, p_int)
            return (QValidator.Invalid, p_str, p_int)
    def fixup(self, p_str):
        print(p_str)
        try:
            if int(p_str) < 18:
                return "18"
            return "180"
        except:
            return "18"

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QAbstractSpinBox")
        self.resize(500, 500)
        self.setup_ui()
    def setup_ui(self): # 添加子控件以及子控件的配置操作
        # pass
        asb = MyASB(self)
        self.asb = asb
        asb.resize(100,30)
        asb.move(100,100)
        # asb.setAccelerated(True) 打开这个方法可以累加加速
        # print(asb.isAccelerated())
        # asb.setReadOnly(True) # 设置只能通过步长调节器调节，不能直接在文本框输入
        # print(asb.isReadOnly())
        asb.editingFinished.connect(lambda :print("结束编辑"))

        test_btn = QPushButton(self)
        test_btn.setText("按钮")
        test_btn.move(200,200)
        test_btn.clicked.connect(self.btn_test)
    def btn_test(self):
        print(self.asb.text())
        # print(self.asb.lineEdit().text())
        # self.asb.lineEdit().setText("888")
        cl = QCompleter(["sz","123","18"],self.asb)
        self.asb.lineEdit().setCompleter(cl)
        # self.asb.lineEdit().setAlignment(Qt.AlignCenter)
        self.asb.setAlignment(Qt.AlignCenter) # 不加lineEdit也可以调用此方法
        # self.asb.setFrame(True) # 默认既是有边框的
        # self.asb.clear() # 清空文本内容
        self.asb.setButtonSymbols(QAbstractSpinBox.NoButtons)
        """
        枚举值如下：
        NoButtons = 2
        PlusMinus = 1
        UpDownArrows = 0
        """




# 1、创建一个应用程序对象
app = QApplication(sys.argv)

# 2、控件操作
# 创建控件、设置控件、事件和信号的处理
# 2.1 创建控件
window = Window()
# 2.2 设置控件


# 2.3 展示控件
window.show()

# 3、应用程序的执行，进入到消息循环
sys.exit(app.exec_())